package com.cxs.utils;

/**
 * @Project: jdbc
 * @Author: cxs2014501@163.com
 * @Create: 2022/8/31 10:15
 * @Description:
 **/
public class UnderlineToHumpCaseUtil {

    private static final Character FLAG =  '_' ;

    /**
     * 下划线->小驼峰
     * @param underLine
     * @return
     */
    public static String underlineToHumpCase(String underLine){
        if (null == underLine || underLine.length() == 0) {
            return underLine;
        }
        StringBuilder sb = new StringBuilder();
        int pos = 0;
        while (pos < underLine.length()){
            Character c = underLine.charAt(pos);
            if (c == FLAG) {
                c = underLine.charAt(++pos);
                sb.append(Character.toUpperCase(c));
            } else {
                sb.append(c);
            }
            pos++;
        }
        return sb.toString();
    }

    /**
     * 小驼峰->下划线
     * @param humpCase
     * @return
     */
    public static String humpCaseToUnderline(String humpCase){
        if (null == humpCase || humpCase.length() == 0) {
            return humpCase;
        }
        StringBuilder sb = new StringBuilder();
        int pos = 0;
        while (pos < humpCase.length()){
            Character c = humpCase.charAt(pos);
            int code = (int) c;
            if ( code >= 65 && code <= 90 ) {
                if (pos == 0) {
                    sb.append(Character.toLowerCase(c));
                } else {
                    sb.append(FLAG);
                    sb.append(Character.toLowerCase(c));
                }

            } else {
                sb.append(c);
            }
            pos++;
        }
        return sb.toString();
    }
}
